---
title: "Replication_File_for_WILS_PSPaper.Rmd"
author: "Eleanor Neff Powell, Leslie Schwindt-Bayer, and Gisela Sin"
date: "`r Sys.Date()`"
output:
   pdf_document:
     latex_engine: xelatex
---


# Setup and Loading Dataset 
```{r setup, include=TRUE}
knitr::opts_chunk$set(echo = FALSE)
library(tidyverse)
library(haven)
library(ggplot2)
library(naniar)
library(ggpubr)
library(tidyr)
library(dplyr)
library(reshape2)
library(lemon)
library(summarytools)
library(knitr)
library(xtable)
library(formattable)
library(pander)
library(flextable)
library(magrittr)

```


```{r data, include=TRUE}
### This codefile assumes you have placed this file and the dataset in the same folder. 
### If not, you'll likely need to set your working directory to load the file. 


#### Opening the Leg Survey Data File
load(file='wils2023fordistribution.Rdata')
```


# Table 1: Descriptives on Subfield, Rank, Race 
Note: To preserve the anonymity of our respondents three demographic variables (academic rank,subfield, and race) have been redacted from this dataset. These variables 
are only used to create Table 1 in the paper. Therefore the code to create this table does not run. 

```{r table1, eval=FALSE, include=TRUE}


###### Table 1: Descriptives on Subfield, Rank, Race

### Creating Academic Rank variable that combines some response categories


#wils2023$acadrank<-recode_factor(wils2023$Q15, "Full Professor"="Full/Emeritus Professor", "Assistant Professor"="Assistant Professor", "Associate Professor"="Associate Professor",
                                         "Adjunct Professor"="Adjunct/Instructor/Other", "Graduate Student"="Grad Student/Post-Doc","Other. Please describe:"="Adjunct/Instructor/Other",
                                         "Post-Doc"="Grad Student/Post-Doc","Instructor"="Adjunct/Instructor/Other","No Response"="No Response", "Emeritus Professor"="Full/Emeritus Professor")
## Checking the recode
#table(wils2023$acadrank, wils2023$Q15)

### Academic Rank Table
### sorting by most common academic rank
#t1<-round(100*prop.table(sort(table(wils2023$acadrank,useNA="ifany"),decreasing=TRUE)),digits=0) ## for all respondents
#rn<-rownames(t1)
#rank_levels<-rn
#t1<-paste(t1,"%",sep="")
#tmp1<-addmargins(table(wils2023$Q15))
#t1<-c(t1,tmp1[length(tmp1)])
#t1<-as.matrix(t1)
#rownames(t1)<-c(rn,"Number of Observations")
#rn<-rownames(t1)
#t1<-as.data.frame(cbind(rn,t1[,1]))

### For export to latex
#print(xtable(t1, caption="Academic Rank"),caption.placement = "top")


### Subfield Table
### sorting by most common academic rank
#t2<-as.table(matrix(NA,6,1))
#t2[1,1]<-round(100*table(wils2023$Q2_1)[2]/sum(table(wils2023$anysubfield)),digits=0)
#t2[2,1]<-round(100*table(wils2023$Q2_2)[2]/sum(table(wils2023$anysubfield)),digits=0)
#t2[3,1]<-round(100*table(wils2023$Q2_3)[2]/sum(table(wils2023$anysubfield)),digits=0)
#t2[4,1]<-round(100*table(wils2023$Q2_4)[2]/sum(table(wils2023$anysubfield)),digits=0)
#t2[5,1]<-round(100*table(wils2023$Q2_5)[2]/sum(table(wils2023$anysubfield)),digits=0)
#t2[6,1]<-round(100*table(wils2023$anysubfield)[1]/sum(table(wils2023$anysubfield)),digits=0)

#rownames(t2)<-c("American","Comparative","International Relations",
                "Political Philosophy","Political Methodology","No Response")
#t2<-paste(t2,"%",sep="")
#tmp2<-addmargins(table(wils2023$Q2_1))
#t2<-c(t2,sum(table(wils2023$anysubfield)))
#t2<-as.matrix(t2)
#rownames(t2)<-c("American","Comparative","International Relations",
                "Political Philosophy","Political Methodology","No Response","Number of Observations")
### For export to latex
#print(xtable(t2, caption="Political Science Subfield - Note can identify with multiple subfields"),caption.placement = "top")

#t2<-as.data.frame(cbind(rownames(t2),t2[,1]))


#### Race/Ethnicity 
#t3<-round(100*prop.table(sort(table(wils2023$Q24),decreasing=TRUE)),digits=0)
#rn<-rownames(t3)
#rank_levels<-rn
#t3<-paste(t3,"%",sep="")
#tmp1<-addmargins(table(wils2023$Q24))
#t3<-c(t3,tmp1[length(tmp1)])
#t3<-as.matrix(t3)
#rownames(t3)<-c(rn,"Number of Observations")
### For export to latex
#print(xtable(t3, caption="Race/Ethnicity"),caption.placement = "top")
#t3<-as.data.frame(cbind(rownames(t3),t3[,1]))


#table1<-rbind(t1,t2,t3)
#kable(table1,caption = "Table 1: Descriptives on Subfield, Rank, Race ")
#tableh1<-cbind(t1,t2,t3)


#rownames(tableh1)<-NULL
#names(tableh1)<-c("Academic Rank", "r%", "Subfield", "s%", "Race", "rc%")
#kable(tableh1,caption = "Table 1: Descriptives on Subfield, Rank, Race ")
#if( require("xtable") ){
#  tableh1 <- xtable(tableh1)
#  xtable::align(tableh1) <- "clr|lr|lr"
#  hlines <- c(-1, 0, 6, nrow(tableh1))
#  ft <- as_flextable(tableh1, hline.after = hlines, include.rownames=FALSE)
#  ft
#}

```



# Table 2: Do you consider yourself someone who studies legislatures? 
```{r table2}
#### Do you consider yourself someone who 'studies legislatures'?"
t1<-round(100*prop.table(table(wils2023$Q5)),digits=0)
rn<-rownames(t1)
t1<-paste(t1,"%",sep="")
tmp1<-addmargins(table(wils2023$Q5))
t1<-c(t1,tmp1[length(tmp1)])
t1<-as.matrix(t1)
rownames(t1)<-c(rn,"n")
t1<-as.data.frame(t1)
# for latex
#print(xtable(t1, caption="Do you consider yourself someone who 'studies legislatures'?"),caption.placement = "top")
#kable(t1)
names(t1)<-c( "%")
if( require("xtable") ){
  t1 <- xtable(t1)
  xtable::align(t1) <- "lr"
  hlines <- c(-1, 0, nrow(t1))
  ft <- as_flextable(t1, hline.after = hlines)
  ft
}


```


##### Figure 1: How respondents describe the proportion of their work related to legislatures by how they self-identify as legislative scholars
```{r figure1}
positions<-c("Definitely not", "Probably not", "Not Sure", "Probably yes", "Definitely yes")
p<-ggplot(data=subset(wils2023,!is.na(identityrank)),aes(x=identityrank,y=Q4_1))+
  stat_summary_bin(fun = "mean", geom = "bar",na.rm=TRUE)+
  theme(axis.text=element_text(size=12), axis.title=element_text(size=14,face="bold"))+
  ylab("What % of your work is about legislatures?")+
  xlab("Do you consider yourself someone who studies legislatures?")+
  ylim(c(0,100))+
  geom_hline(aes(yintercept=50))

tiff(filename="figure1proportionleg.tiff", width=480, height=480)
p
dev.off()

```



##### Table 3: LSS, LSQ, and Submission Data

```{r table3}
### LSS Membership
t1<-round(100*prop.table(sort(table(wils2023$lss,useNA="ifany"),decreasing=TRUE)),digits=0) ## for all respondents
rn<-rownames(t1)
rank_levels<-rn
t1<-paste(t1,"%",sep="")
tmp1<-addmargins(table(wils2023$lss))
t1<-c(t1,tmp1[length(tmp1)])
t1<-as.matrix(t1)
rownames(t1)<-c(rn,"Number of Observations")
rn<-rownames(t1)
t1<-as.data.frame(cbind(rn,t1[,1]))


##Q26: Have you ever submitted work (solo or co-authored) to LSQ?

t2<-round(100*prop.table(table(wils2023$Q26)),digits=0)
rn<-rownames(t2)
t2<-paste(t2,"%",sep="")
tmp1<-addmargins(table(wils2023$Q26))
t2<-c(t2,tmp1[length(tmp1)])
t2<-as.matrix(t2)
rownames(t2)<-c(rn,"n")
t2<-as.matrix(t2[order(t2[,1],decreasing=TRUE),])
t2<-as.data.frame(cbind(rownames(t2),t2[,1]))

t3<-as.table(matrix(NA,5,1))
n28<-sum(wils2023$Q28ALL>0)




for (i in 1:5){
  if (length((unique(wils2023[,grep("Q28_",names(wils2023))[i]])))>1){
    t3[i,1]<-round(100*table(wils2023[,grep("Q28_",names(wils2023))[i]])/n28,digits=0)
    rownames(t3)[i]<-rownames(table(wils2023[,grep("Q28_",names(wils2023))[i]]))    
  }
}
colnames(t3)<-""
t3<-as.matrix(t3[order(t3[,1],decreasing=TRUE),])
t3[,1]<-paste(t3[,1],"%",sep="")
t3<-rbind(t3,n28)
rownames(t3)[nrow(t3)]<-c("Number of Respondents Who Answered This Question")
# for latex
#print(xtable(t1, caption="To which journals do you submit your work before considering LSQ? (Can check multiple)"),caption.placement = "top")
#kable(t3, caption="To which journals do you submit your work before considering LSQ? (Can check multiple)")
t3<-as.data.frame(cbind(rownames(t3),t3[,1]))
#t3
#dim(t3)

#### Now combining all three into a single table
tableh3<-as.data.frame(matrix(data=NA, nrow=6,ncol=6))
## first adding data from subtable 1
tableh3[1:3,1:2]<-t1
## then adding data from subtable 2
tableh3[1:4,3:4]<-t2
## then adding data from subtable 3
tableh3[1:6,5:6]<-t3
#tableh3



rownames(tableh3)<-NULL
names(tableh3)<-c("LSS", "l%", "LSQ", "q%", "Journal", "j%")
#kable(tableh1,caption = "Table 1: Descriptives on Subfield, Rank, Race ")

tableh3$Journal<-recode(tableh3$Journal, "The Journal of Politics"="JOP", "Other:"="Other", "American Journal of Political Science"="AJPS", "American Political Science Review"="APSR", "Legislative Studies Quarterly is typically the first outlet I send my work to"="LSQ","Number of Respondents Who Answered This Question"="n")
tableh3$LSS<-recode(tableh3$LSS, "Non-LSS Member"="Non-LSS Member", "LSS Member"="LSS Member", "Number of Observations"="n")


if( require("xtable") ){
  tableh3 <- xtable(tableh3)
  xtable::align(tableh3) <- "clr|lr|lr"
  hlines <- c(-1, 0, 6, nrow(tableh3))
  ft <- as_flextable(tableh3, hline.after = hlines, include.rownames=FALSE)
  ft
}



```



#### Table 4: What Sections are Non-LSS Members in? 
```{r table4}
#### For those not in LSS, what sections are they in?  
# Q8_3 is the legislative politics section
nonlss<-subset(wils2023, is.na(wils2023$Q8_3)==TRUE)

t1<-as.table(matrix(NA,46,1))

for (i in 1:46){
  if (length((unique(nonlss[,grep("Q8_",names(nonlss))[i]])))>1){
    t1[i,1]<-round(100*table(nonlss[,grep("Q8_",names(nonlss))[i]])/nrow(nonlss),digits=0)
    rownames(t1)[i]<-rownames(table(nonlss[,grep("Q8_",names(nonlss))[i]]))    
  }
}


### Adding Zeros
t1[3,]<-c(0)
rownames(t1)[3]<-"3. Legislative Studies"
t1[15,]<-c(0)
rownames(t1)[15]<-"17. Foundations of Political Theory"
t1[16,]<-c(0)
rownames(t1)[16]<-"18. Information Technology and Politics"
t1[21,]<-c(0)
rownames(t1)[21]<-"23. Political Communication"
t1[24,]<-c(0)
rownames(t1)[24]<-"27. New Political Science"
t1[31,]<-c(0)
rownames(t1)[31]<-"34. International History and Politics"
t1[33,]<-c(0)
rownames(t1)[33]<-"36. Human Rights"
t1[43,]<-c(0)
rownames(t1)[43]<-"46. Ideas, Knowledge, and Politics"
t1[44,]<-c(0)
rownames(t1)[44]<-"47. American Political Thought"
t1[45,]<-c(0)
rownames(t1)[45]<-"48. International Collaboration"


t1<-as.table(t1[order(t1[,1],decreasing=TRUE),])
rn<-rownames(t1)

t1<-as.data.frame(cbind(rownames(t1),t1))
rownames(t1)<-NULL
names(t1)<-c("Section", "m")

### including only sections with more than 5%
t1$m<-as.numeric(as.character(t1$m))
t1<-t1[t1$m>=5,]



## adding % signs
t1$m<-paste(t1$m,"%",sep="")
t1[nrow(t1)+1,]<-c("Number of Observations",nrow(nonlss))


# for latex
#print(xtable(t1, caption="What Sections Non-LSS Members Are In"),caption.placement = "top")
#kable(t1, caption="What Sections Non-LSS Members Are In")


if( require("xtable") ){
  table4 <- xtable(t1)
  xtable::align(table4) <- "llr"
  hlines <- c(-1, 0, 11, nrow(table4))
  ft <- as_flextable(table4, hline.after = hlines, include.rownames=FALSE)
  ft
}





```


#### Table 5: Why Women Don't Join LSS 
```{r table5}
t1<-as.table(matrix(NA,12,1))
for (i in 1:nrow(wils2023)){
  wils2023$Q9ALL[i]<-sum(is.na(wils2023[i,grep("Q9_",names(wils2023))])==FALSE) 
}
nq9<-sum(wils2023$Q9ALL>0)

for (i in 1:12){
  if (length((unique(wils2023[,grep("Q9_",names(wils2023))[i]])))>1){
    t1[i,1]<-round(100*table(wils2023[,grep("Q9_",names(wils2023))[i]])/nq9,digits=0)
    rownames(t1)[i]<-rownames(table(wils2023[,grep("Q9_",names(wils2023))[i]]))    
  }
}
t1<-as.matrix(t1[order(t1[,1],decreasing=TRUE),])
colnames(t1)<-""
t1[,1]<-paste(t1[,1],"%",sep="")
t1<-rbind(t1,nq9)
rownames(t1)[nrow(t1)]<-c("Number of Observations")
# for latex
#print(xtable(t1, caption="Why did you decide not to become a member of APSA’s LSS? (please check all that apply)",caption.placement = "top"))
#kable(t1, caption="Why did you decide not to become a member of APSA’s LSS? (please check all that apply)")
t1<-as.data.frame(t1)
t1$Response<-rownames(t1)
rownames(t1)<-NULL

if( require("xtable") ){
  table5 <- xtable(t1)
  xtable::align(table5) <- "lrr"
  hlines <- c(-1, 0, 12, nrow(table5))
  ft <- as_flextable(table5, hline.after = hlines, include.rownames=FALSE)
  ft
}



```


#### Why Women Don't Join LSS Exclusion Concerns Aggregated
```{r include=TRUE}
nq9<-sum(wils2023$Q9ALL>0)
wils2023$Q9agged<-0
wils2023$Q9agged[wils2023$Q9ALL==0]<-NA
wils2023$Q9agged[wils2023$Q9_4=="Not a welcoming section" | wils2023$Q9_5=="Perceived to be male-dominated" | wils2023$Q9_6=="Lacks racial and ethnic diversity" | wils2023$Q9_7=="Too cliquey/exclusive" |wils2023$Q9_9=="Too heavily focused on quantitative methods" ]<-1
#sum(wils2023$Q9agged,na.rm=TRUE)/nq9
print(round(sum(wils2023$Q9agged,na.rm=TRUE)/nq9, digits=3))


```


#### Table 6: Which of the following do you think are problems in the Legislative Studies Section?  (Select all that apply.)   
```{r table6}
t1<-as.table(matrix(NA,9,1))

wils2023$Q13ALL<-NA
for (i in 1:nrow(wils2023)){
  wils2023$Q13ALL[i]<-sum(is.na(wils2023[i,grep("Q13_",names(wils2023))])==FALSE) 
}
n13<-sum(wils2023$Q13ALL>0)

for (i in 1:9){
  if (length((unique(wils2023[,grep("Q13_",names(wils2023))[i]])))>1){
    t1[i,1]<-round(100*table(wils2023[,grep("Q13_",names(wils2023))[i]])/n28,digits=0)
    rownames(t1)[i]<-rownames(table(wils2023[,grep("Q13_",names(wils2023))[i]]))    
  }
}
colnames(t1)<-""
t1<-as.matrix(t1[order(t1[,1],decreasing=TRUE),])
t1[,1]<-paste(t1[,1],"%",sep="")
t1<-rbind(t1,n28)
rownames(t1)[nrow(t1)]<-c("Number of Respondents Who Answered This Question")

t1<-as.data.frame(t1)
t1$Response<-rownames(t1)
rownames(t1)<-NULL




if( require("xtable") ){
  table6 <- xtable(t1)
  xtable::align(table6) <- "lrr"
  hlines <- c(-1, 0, 9, nrow(table6))
  ft <- as_flextable(table6, hline.after = hlines, include.rownames=FALSE)
  ft
}




```

